var basePage = require('../../../common/BasePage');
var util = require('../../../utils/util');
Page({
  behaviors: [basePage],
  data: {
    /**订单数据 */
    pdc: {},
    /**评分 */
    evaluate_level: '',
    /**评论内容 */
    content: '',

    img_fileList: [],
    video_fileList: [],


  },
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel()
    let that = this;
    eventChannel.on('pdcData', function (data) {
      console.log("要评价的产品数据", data);
      that.setData({
        pdc: data
      })
    })
  },
  Image_afterRead(event) {
    let that=this;
    const file= event.detail.file;
    let img_fileList  = this.data.img_fileList;
    // console.log("file",file);
    img_fileList.push(file);
    file.status="uploading";
    this.setData({
      img_fileList
    });
    wx.uploadFile({
      filePath: file.url,
      name: 'image',
      url: that.data.host + '/home/upload/image',
      formData: {
        from: 'comment'
      },
      success(res) {
       
        file.status="failed";
        console.log("上传成功",res);

        if (res.statusCode != 200) {

          return wx.showModal({
            showCancel: false,
            content: res.errMsg || '上传失败',
          })
        }
        let resObj = JSON.parse(res.data);
        if (resObj.code) {
        
          return wx.showToast({
            title: resObj.msg,
            icon: 'none'
          })
        }
        file.status="done";

        file.photo = resObj.data.path;

      },
      fail() {
        file.status="failed";
      },
      complete() {
        that.setData({img_fileList})
      }
    });
   
  },
  delete_image(e) {
    let {
      img_fileList = []
    } = this.data;
    img_fileList.splice(e.detail.index, 1);
    this.setData({
      img_fileList
    });

  },
  video_afterRead(event) {
    let that=this;
    const file= event.detail.file;
    let img_fileList  = this.data.img_fileList;
    // console.log("file",file);
    file.isVideo=true;
    if(img_fileList.length&&img_fileList[0].isVideo){
      img_fileList[0]=file
    }else{
      img_fileList.unshift(file);
    }
    
    file.status="uploading";
    this.setData({
      img_fileList
    });
    wx.uploadFile({
      filePath: file.url,
      name: 'file',
      url: that.data.host + '/home/upload/video',
      formData: {
        from: 'comment'
      },
      success(res) {
        file.status="failed";
        console.log("上传成功",res);
        if (res.statusCode != 200) {
          return wx.showModal({
            showCancel: false,
            content: res.errMsg || '上传失败',
          })
        }
        let resObj = JSON.parse(res.data);
        if (resObj.code) {
          return wx.showToast({
            title: resObj.msg,
            icon: 'none'
          })
        }
        file.status="done";
        file.photo = resObj.data.path;
      },
      fail() {
        file.status="failed";
      },
      complete() {
        that.setData({img_fileList})
      }
    });
  },
  onChange(event) {
    // console.log("评分", event.detail);
    this.setData({
      evaluate_level: event.detail,
    });
  },
  /**
   * 提交评价
   */
  submit_click() {
    console.log(this.data.img_fileList);
    // return;
    // console.log("评论内容",{score:this.data.evaluate_level,content:this.data.content});
    if (!this.data.evaluate_level) {
      return wx.showToast({
        title: '请打评分',
        icon: 'none'
      })
    }
    let photoArr=[];
    let videos="";
    this.data.img_fileList.map(val=>{
      if(val.isVideo){
        videos=val.photo;
      }else{
        photoArr.push(val.photo);
      }
    })
    this.data.content= util.replaceStr(this.data.content)
    this.request({
      url: this.data.host + '/home/order/comment',
      data: {
        order_id: this.data.pdc.order_id,
        order_pid: this.data.pdc.order_product_id,
        score: this.data.evaluate_level,
        content: this.data.content,
        videos:videos,
        photos:photoArr.join(",")
      },
      method: "POST",
      loadingTip: '加密提交...'
    }, res => {
      wx.showModal({

        content: '提交成功！',
        showCancel: false,
        title: '恭喜',
        success: (result) => {
          wx.navigateBack({
            delta: 1,
          })
        },
      })
      //  console.log("提交评论",res)
    })
  }
})